package com.easybenefit.UUService.fragment;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UULogUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.model.MerchantBean;
import com.easybenefit.UUService.model.MerchantsInfoBean;
import com.easybenefit.UUService.myinterface.IActivityInterface;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class MerchantsIntroduceFragment extends Fragment implements IActivityInterface {

    private Context context;
    private WebView webView;
    private View contentView;
    private MerchantBean merchantBean;
    private MerchantsInfoBean merchantsInfoBean;
    private ProgressDialog webProgressDialog;
    public static MerchantsIntroduceFragment newInstance() {
        MerchantsIntroduceFragment fragment = new MerchantsIntroduceFragment();
        fragment.setRetainInstance(true);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        contentView = inflater.inflate(R.layout.fragment_merchants_introduce, container, false);
        initView();
        initData();
        initListener();
        return contentView;
    }


    @Override
    public void initData() {
        context=getActivity();
        String data = merchantsInfoBean.getMer_desc();
        data = data.replace("img src=\"", "img src=\"" + UUConfig.BASE_URL);
        data += "<style>\n" +
                "  \n" +
                "img{\n" +
                " max-width:100%;\n" +
                " height:auto;\n" +
                "}\n" +
                "  \n" +
                "</style>";
        UULogUtil.e("data", data);
        webView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
        webProgressDialog = UUDialogUtill.getLoadingBar(context, "正在加载请稍后....");
        webProgressDialog.show();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress==100){
                    webProgressDialog.dismiss();
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    public void initView() {
        webView = (WebView) contentView.findViewById(R.id.webView);
        // webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    public void initListener() {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    public MerchantsInfoBean getMerchantsInfoBean() {
        return merchantsInfoBean;
    }

    public void setMerchantsInfoBean(MerchantsInfoBean merchantsInfoBean) {
        this.merchantsInfoBean = merchantsInfoBean;
    }
}
